<template>
  <p-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <p-spin :spinning="confirmLoading">
      <p-form :form="form">
        <p-row>
          <p-col :span="24">
            <p-form-item label="工序工单" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="['moid']" placeholder="请输入工序工单"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="SN条码(旧)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="['barCode']" placeholder="请输入SN条码(旧)"></p-input>
            </p-form-item>
          </p-col>
          <p-col :span="24">
            <p-form-item label="SN条码(新)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <p-input v-decorator="[ 'processCoding']" placeholder="请输入SN条码(旧)"></p-input>
            </p-form-item>
          </p-col>
        </p-row>
      </p-form>
    </p-spin>
  </p-modal>
</template>

<script>
import { httpAction, putAction, api } from "@/api/manage";
import { SubmitOnchangMixin } from "@/mixins/SubmitOnchangMixin";
import pick from "lodash.pick";
import { validateDuplicateValue } from "@/utils/util";
import { postAction } from "../../../../api/manage";
export default {
  name: "",
  mixins: [SubmitOnchangMixin],
  data() {
    return {
      form: this.$form.createForm(this),
      title: "操作",
      width: 800,
      visible: false,
      model: {},
      confirmLoading: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      url: {
        replace: "/mes-manufacture/mfProcessCriticalComponentBinding/saveKeyMaterialExchange", //实现替换
      },
      record: {},
    };
  },
  methods: {
    add(s) {
      console.log(s);
      this.edit(s);
      // this.getRuleCodeValue(); //自动获取替换的数据SN
    },
    //替换关键物料
    // replace() {
    //   let params = {};
    //   postAction(this.url.replace, params).then((res) => {
    //     if (res.msg !== "success") return this.$message.warning(res.msg);
    //     this.$message.success(res.msg);
    //     this.getBind_recordlist();
    //   });
    // },
    getRuleCodeValue() {
      let param = {
        commonFormData: this.model,
        rules: [
          { ruleCode: "mes_label_template_no", columnName: "labelTemplateNo" },
        ],
      };

      putAction(api.ruleBaseURL, param).then((res) => {
        // 执行成功，获取返回的值，并赋到页面上
        if (res.success) {
          console.log("生成的编码：" + res.data);
          const item = res.data;
          this.form.setFieldsValue(pick(item, Object.keys(item)));
        }
      });
    },

    edit(record) {
      this.record = record;
      this.form.resetFields();
      this.model = Object.assign({}, record);
      this.visible = true;
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.model, Object.keys(record)));
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true;
          // let formData = Object.assign(this.model, values);
          // this.formData = Object.assign(this.model, values);
          let formData = {
            bindingNew: values.processCoding,
            id: this.record.id,
          };
          httpAction(that.url.replace, formData, "POST")
            .then((res) => {
              if (res.success) {
                that.$message.success(res.msg);
                that.$emit("ok");
              } else {
                that.$message.warning(res.msg);
              }
            })
            .finally(() => {
              that.confirmLoading = false;
              that.close();
            });
          // if (this.EnhanceJS.afterAdd) {
          //   //新增之前
          //   let Obj = eval(this.EnhanceJS.afterAdd);
          //   new Obj(this);
          // } else {
          //   console.log("表单提交数据", formData);
          // that.modalFrom(httpurl, formData, method);
          //}
          // 新增触发;
          // if (!this.model.id) {
          //   httpurl += this.url.add;
          //   method = "post";
          //   if (this.EnhanceJS.afterAdd) {
          //     //新增之前
          //     let Obj = eval(this.EnhanceJS.afterAdd);
          //     new Obj(this);
          //   } else {
          //     formData.routerStatus = "1";
          //     console.log("表单提交数据", formData);
          //     that.modalFrom(httpurl, formData, method);
          //   }
          // } else {
          //   //编辑触发
          //   httpurl += this.url.edit;
          //   method = "post";
          //   if (this.EnhanceJS.afterEdit) {
          //     //编辑之前
          //     let Obj = eval(this.EnhanceJS.afterEdit);
          //     new Obj(this);
          //   } else {
          //     formData.routerStatus = "2";
          //     console.log("表单提交数据", formData);
          //     that.modalFrom(httpurl, formData, method);
          //   }
          // }
        }
        console.log(values);
      });
    },
    handleCancel() {
      this.close();
    },
    popupCallback(row) {
      this.form.setFieldsValue(pick(row, "mtype", "mcode", "mname", "qty"));
    },
  },
};
</script>

<style lang='scss' scoped>
</style>
